#!/opt/ActiveTcl-8.6/bin/tclsh8.6  

#load file using the relative path of the current script
proc source2 { fname} {
   global loaded
   if ![info exists loaded] { set loaded {}}
   set mydir [file dirname [dict get [info frame [info frame]] file ]]
   foreach f [list [file join $mydir $fname] $fname ] {
      if [file exists $f] {set fname $f; break}
   }
   if {[lsearch $loaded $fname] >=0} { return}
   set olddir [pwd]
   #puts "loaded $fname"
   cd [file dirname $fname]
   lappend loaded $fname
   uplevel 1 "source $fname"
   cd $olddir
}


source2 DefForm.tcl
source2 PackForm.tcl
source2 setup.tcl

# generate option forms 
ttk::notebook .options
set Container(form) .options
foreach form $Child(form) {
    packForm $form .options
    packChild $form
}
ttk::labelframe .image -text "Shape Diagram" -height 320 -width 400 
ttk::frame .control -height 50 -width 820
ttk::labelframe .log -text "Log" -height 100 -width 1020
text .log.windows -height 20 -bg black -fg white -state disabled -yscrollcommand ".log.yscroll set" -width 120
ttk::scrollbar .log.yscroll -command ".log.windows yview" 
pack .log.windows .log.yscroll -side left -fill y -expand yes -padx 2 

rename ::puts ::realputs 
proc puts { args } {
    if {[llength $args] < 2} {
	.log.windows configure -state normal
	.log.windows insert end "[lindex $args end]\n"
	.log.windows configure -state disabled
    } else {
	uplevel 1 ::realputs $args
    }
} 

set workDir [pwd]
ttk::frame .control.path
ttk::label .control.path.name -text "Working Directory: "
ttk::entry .control.path.value -width 40 -textvariable workDir
ttk::button .control.path.open -text "Open" -command {set workDir [tk_chooseDirectory -initialdir $workDir];}
grid .control.path.name .control.path.value .control.path.open -padx 10


ttk::button .control.start -text "Start" -command {
    if ![file exists $workDir] {
       set reply [tk_dialog .createNewDir "Create New Dir?" "$workDir is not there! Do you want to create one? If not, no job will be created." \
       questhead 0 Yes No]
       if { $reply == 0 } {
         if ![catch { package require tkMD}] {
           Path::mk_path $workDir
           cd $workDir
           AddJob "[genJob]"
         } else {
            tk_messageBox -message "tkMD do not exists for this system!Will not add" -type ok
         }
       }
       return 
    }
    cd $workDir
    AddJob "[genJob]"
}
ttk::button .control.stop -text "Stop" -command {
	DelJob
 }

 
pack .control.path .control.start .control.stop -side left -anchor e -padx 10
tooltip::tooltip .control.start "Start Calculation"
#bind . <Configure> { puts "%W is now %w x %h"}
bindall .control <Button-1> {puts "%W clicked"}
wm title . "Build Membrane"
grid .options -row 0 -column 0  -padx 10 -pady 10
grid .image  -row 0 -column 1 -padx 10 -pady 10 
grid .control - - -sticky w -padx 10 
grid .log -columnspan 4  -padx 10 -pady 10 -sticky nw
grid columnconfigure . 1 -weight 1 
grid rowconfigure . 2 -weight 1
puts "Welcome $::env(USER)!"
puts "Please change the working directory to desired directory, setup the membrane and press Start."
update 
proc every {ms body } {eval $body; after $ms [info level 0]}
every 5000 { refreshJobs;refreshField jobs.list.jobstatus; update}
